rm(list = ls())

library(SetMethods)

## Import calibrated data
DFnew <- read.csv("Calibrated data.csv", header = T, row.names = 1)
head(DFnew)

#### Analyses for MARCOMB ####

### Necessity for MARCOMB
SUIN_ymc <- superSubset(data = DFnew, 
                        outcome = "MARCOMB",
                        conditions = c("MOS", "UNSOS", "POL", "RES"),
                        relation = "nec", 
                        incl.cut = 0.8, 
                        cov.cut = 0.5, 
                        ron.cut = 0.5)
SUIN_ymc

pimplot(data = DFnew, results = SUIN_ymc, outcome = "MARCOMB", 
        necessity = T, all_labels = T, jitter = T)


### Sufficiency for MARCOMB
## Create truth table 
TT_ymc <- truthTable(DFnew, outcome = "MARCOMB",
                     conditions = c("MOS", "UNSOS", "POL", "RES"),
                     incl.cut = 0.75, 
                     show.cases = T, 
                     sort.by = c("out", "incl", "n"),
                     complete = T)
TT_ymc

# Remove TT rows with low PRI
TT_ymc$tt[c("5", "8"), "OUT"] <- 0
TT_ymc

### Minimise
sol_ymcc <- minimize(TT_ymc, use.tilde = T, 
                     details = T)
sol_ymcc

sol_ymcp <- minimize(TT_ymc, use.tilde = T, 
                     details = T, 
                     include = "?")
sol_ymcp

sol_ymci <- minimize(TT_ymc, 
                     details = T, 
                     include = "?", 
                     dir.exp = c("1, 1, 1, 1"))
sol_ymci

pimplot(data = DFnew, results = sol_ymci, outcome = "MARCOMB",
        all_labels = T, jitter = T)

pimplot(data = DFnew, results = sol_ymci, outcome = "MARCOMB", 
        ttrows = c("14","12"), all_labels = T, jitter = T)

#### Analyses for ~MARCOMB ####

### Necessity for ~MARCOMB
SUIN_nmc <- superSubset(data = DFnew, 
                        outcome = "~MARCOMB",
                        conditions = c("MOS", "UNSOS", "POL", "RES"),
                        relation = "nec", 
                        incl.cut = 0.8, 
                        cov.cut = 0.5, 
                        ron.cut = 0.5)
SUIN_nmc

pimplot(data = DFnew, results = SUIN_nmc, outcome = "~MARCOMB", 
        neg.out = T, necessity = T, all_labels = T, 
        jitter = T)

### Sufficiency for ~MARCOMB (= STATE)
## Create truth table
tt_nmc <- truthTable(DFnew, 
                     outcome = "MARCOMB", 
                     conditions = c("MOS", "UNSOS", "POL", "RES"),
                     incl.cut = 0.750, 
                     show.cases = T, 
                     sort.by = c("out", "n", "incl"), 
                     neg.out = T)
tt_nmc

# Remove TT rows with low PRI
tt_nmc$tt[c("5"), "OUT"] <- 0
tt_nmc

## Minimise
sol_nmcc <- minimize(tt_nmc, use.tilde = T, details = T)
sol_nmcc

sol_nmcp <- minimize(tt_nmc, use.tilde = T, details = T, 
                     include = "?")
sol_nmcp

sol_nmci <- minimize (tt_nmc, details = T, 
                      include = "?", dir.exp = c("0, 0, 0, 0"))
sol_nmci

pimplot(data = DFnew, results = sol_nmci, outcome = "MARCOMB", 
        neg.out = T, jitter = T, all_labels = T)


### Enhanced Standard Analysis (tests)
CSAmc<- intersect(rownames(sol_ymci$SA), rownames(sol_nmci$SA))
CSAmc

SSRmc <- intersect(rownames(TT_ymc$tt)[TT_ymc$tt$OUT==1],
                   rownames(tt_nmc$tt)[tt_nmc$tt$OUT==1])
SSRmc

tt_esa <- esa(tt_nmc, nec_cond = "~MOS")
tt_esa

sol_nesami <- minimize(tt_esa, details = T, include = "?", 
                       dir.exp = c("0, 0, 0, 0"))
sol_nesami

pimplot(data = DFnew, results = sol_nesami, outcome = "MARCOMB", 
        neg.out = T, jitter = T, all_labels = T)

##### ANALYSES FOR STACOMB #####

### Necessity for STACOMB
SUIN_ysc <- superSubset(data = DFnew, 
                        outcome = "STACOMB",
                        conditions = c("MOS", "UNSOS", "POL", "RES"),
                        relation = "nec", 
                        incl.cut = 0.8, 
                        cov.cut = 0.5, 
                        ron.cut = 0.5)
SUIN_ysc

pimplot(data = DFnew, results = SUIN_ysc, outcome = "STACOMB", sol = 1,
        necessity = T, jitter = T, all_labels = T)

### Sufficiency for STACOMB
## Create truth table 
TT_ysc <- truthTable(DFnew, outcome = "STACOMB",
                     conditions = c("MOS", "UNSOS", "POL", "RES"),
                     incl.cut = 0.8, 
                     show.cases = T, 
                     sort.by = c("out", "incl", "n"),
                     complete = T)
TT_ysc

write.csv(TT_ysc$tt, "TT STACOMB(without MEMB).csv")

## Minimise

sol_yscc <- minimize(TT_ysc, use.tilde = T, 
                     details = T)
sol_yscc

sol_yscp <- minimize(TT_ysc, use.tilde = T, 
                     details = T, 
                     include = "?")
sol_yscp

sol_ysci <- minimize(TT_ysc, 
                     details = T, 
                     include = "?", 
                     dir.exp = c("0, 0, 0, 0"))
sol_ysci

pimplot(data = DFnew, results = sol_ysci, outcome = "STACOMB", 
        jitter = T, all_labels = T)


#### Analyses for ~STACOMB ####

### Necessity for ~STACOMB
SUIN_nsc <- superSubset(data = DFnew, 
                        outcome = "~STACOMB",
                        conditions = c("MOS", "UNSOS", "POL", "RES", "MEMB"),
                        relation = "nec", 
                        incl.cut = 0.8, 
                        cov.cut = 0.5, 
                        ron.cut = 0.5)
SUIN_nsc

pimplot(data = DFnew, results = SUIN_nsc, outcome = "~STACOMB", 
        neg.out = T, necessity = T,
        all_labels = T, jitter = T)

### Sufficiency for ~STACOMB (= MARKET)
## Create truth table
tt_nsc <- truthTable(DFnew, 
                     outcome = "STACOMB", 
                     conditions = c("MOS", "UNSOS", "POL", "RES"),
                     incl.cut = 0.750, 
                     show.cases = T, 
                     sort.by = c("out", "n", "incl"), 
                     neg.out = T)
tt_nsc

# Exclude TT rows with low PRI
tt_nsc$tt["5", "OUT"] <- 0
tt_nsc

write.csv(tt_nsc$tt, "TT ~STACOMB(Without MEMB).csv")

## Minimise
sol_nscc <- minimize(tt_nsc, use.tilde = T, details = T)
sol_nscc

sol_nscp <- minimize(tt_nsc, use.tilde = T, details = T, 
                     include = "?")
sol_nscp

sol_nsci <- minimize (tt_nsc, details = T, 
                      include = "?", dir.exp = c("1, 1, 1, 1"))
sol_nsci

pimplot(data = DFnew, results = sol_nsci, outcome = "STACOMB", 
        all_labels = T, jitter = T, neg.out = T)

### Enhanced Standard Analysis (tests)
CSAsc<- intersect(rownames(sol_ysci$SA), rownames(sol_nsci$SA))
CSAsc

SSRsc <- intersect(rownames(TT_ysc$tt)[TT_ysc$tt$OUT==1],
                   rownames(tt_nsc$tt)[tt_nsc$tt$OUT==1])
SSRsc

tt_nesa <- esa(tt_nsc, nec_cond = c("MOS", "~POL"))
tt_nesa

sol_nesac <- minimize (tt_nesa, details = T, use.tilde = T)
sol_nesac

sol_nesap <- minimize(tt_nesa, details = T, use.tilde = T, 
                      include = "?")
sol_nesap

sol_nesai<- minimize(tt_nesa, details = T, include = "?", 
                     dir.exp = c("1, 1, 1, 1"))

sol_nesai

pimplot(data = DFnew, results = sol_nesai, outcome = "STACOMB", 
        all_labels = T, jitter = T, neg.out = T)